package com.cuz.daileetcode.h100;

import java.util.HashMap;
import java.util.Map;

public class P2 {
        public int lengthOfLongestSubstring(String s) {
            if (s == null) {
                return 0;
            }
            if (s.length() <= 1) {
                return s.length();
            }
            int left = 0;
            int right = 0;
            int max = 0;
            Map<Character, Integer> map = new HashMap<Character, Integer>();
            while (right < s.length()) {
                char rC = s.charAt(right);
                Integer index = map.get(rC);
                if (index != null) {
                    left = Math.max(left, index + 1);
                }
                map.put(rC, right);
                max = Math.max(max, right - left + 1);
                right++;
            }
            return max;
        }

    public static void main(String[] args) {
        int abcabcbb = new P2().lengthOfLongestSubstring("abcabcbb");
        System.out.println(abcabcbb);
    }
}
